﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Linq;
using System.Xml.Linq;
using System.Reflection;

namespace iCustomDAO
{
    public abstract class aCustomItems
    {
        // public abstract string queryString { get; set; }
        public DataSet ds;
        //public abstract List<Customer> listCustom { get; }

        public aCustomItems()
        {
            // TODO: Complete member initialization
        }

        protected internal string FullName()
        {
            string Msg = "Internal Property";
            return Msg;
        }

        public virtual string Description()
        {
            string Msg = "An item";
            return Msg;
        }
    }

    [DataObject(true)]
    public class Customer : aCustomItems
    {
        public Customer() { }

        //public override string queryString
        //{
        //    get { return ""; }
        //    set { queryString = value; }
        //}

        public int intCodeID { get; set; }
        public string Name { get; set; }
        public string AnalyseName { get; set; }
        public double strQty { get; set; }
        public string Price { get; set; }
        public string Symbol { get; set; }
        public string strSD { get; set; }
        public string strED { get; set; }
        public string strAnalyseID { get; set; }
        public string strCategoryID { get; set; }
        public string strStatus { get; set; }

        public Customer listCustom
        {
            get
            {
                return this;
            }
        }
    }

    public sealed class itemCustomSealed
    {
        public int intMax;
        public string messageTypeObject;
    }

    public class sClassObject : aCustomItems
    {
        //public override string queryString
        //{
        //    get { return ""; }
        //    set { queryString = value; }
        //}
    }

    interface iBaseInterface
    {
        double Area { get; }
    }

    interface eInterface : iBaseInterface
    {
        double Base { get; set; }
    }
}

public class CustomerDAO : iCustomDAO.aCustomItems, iCustomDAO.eInterface
{
    /// <summary>
    /// Implement interface member
    /// </summary>
    /// 
    public bool bForce = false;
    public string customQS = "";
    private DataSet dsList;

    public DataSet DsList
    {
        get { return dsList; }
        set { dsList = value; }
    }

    //public override string queryString
    //{
    //    get { return customQS; }
    //    set { customQS = value; }
    //}

    public double Area
    {
        get { return Base; }
    }

    public double Base
    {
        get { return 0; }
        set { Base = value; }
    }

    public bool bPopulate
    {
        get { return false; }
        set { bPopulate = value; }
    }

    public override string Description()
    {
        string Introduction = base.Description();

        string Msg = Introduction +
                 "\nA description.";
        return Msg;
    }

    /// <summary>
    /// Private List of type Customer
    /// </summary>
    public List<iCustomDAO.Customer> Customers
    {
        get
        {
            DataSet dsTest = null;
            List<iCustomDAO.Customer> customers = HttpContext.Current.Session["Customers"] as List<iCustomDAO.Customer>;

            CommonSession commonSes = HttpContext.Current.Session["CommonSession"] as CommonSession;
            if (commonSes != null)
            {
                bForce = commonSes.bForce;
                //queryString = commonSes.queryString;
            }

            if (customers == null || bForce)
            {
                customers = new List<iCustomDAO.Customer>();
                dsTest = Hipocrate.DB.DBConnection.GetData(commonSes.queryString);
                DataTable contacts = dsTest.Tables[0];

                customers =
                (
                    from user in contacts.AsEnumerable()

                    select new iCustomDAO.Customer
                    {
                        intCodeID = Convert.ToInt32(user["ID"]),
                        Name = user["Name"].ToString(),
                        AnalyseName = user["AnalyseName"].ToString(),
                        strQty = Convert.ToDouble(user["Qty"]),
                        Price = user["Price"].ToString(),
                        Symbol = user["Symbol"].ToString(),
                        strSD = user["StartDate"].ToString(),
                        strED = user["EndDate"].ToString(),
                        strAnalyseID = user["AnalyseID"].ToString(),
                        strCategoryID = user["CostCategoryID"].ToString(),
                        strStatus = "Selected",
                    }
                ).ToList();

                HttpContext.Current.Session["Customers"] = customers;
                commonSes.dsList = dsTest;
            }

            return customers;

        }
    }

    /// <summary>
    /// 
    /// </summary>
    public CustomerDAO() { }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [DataObjectMethod(DataObjectMethodType.Select)]
    public IEnumerable<iCustomDAO.Customer> FindAll()
    {
        return this.Customers;
    }

    /// <summary>
    /// Update
    /// </summary>
    /// <param name="customer"></param>
    public void Update(iCustomDAO.Customer newValues)
    {
        CommonSession commonSes = HttpContext.Current.Session["CommonSession"] as CommonSession;
        if (commonSes != null)
            commonSes.bForce = false;

        iCustomDAO.Customer oldValues = this.Customers.Find(x => x.intCodeID == newValues.intCodeID);
        oldValues.Name = newValues.Name;
        oldValues.strQty = newValues.strQty;
        //oldValues.Price = newValues.Price;
        oldValues.strCategoryID = newValues.strCategoryID;
        oldValues.strSD = newValues.strSD;
        oldValues.strED = newValues.strED;
        oldValues.strStatus = "Updated";
    }

    /// <summary>
    /// Delete Items
    /// </summary>
    /// <param name="customer"></param>
    public void Delete(iCustomDAO.Customer newValues)
    {
        CommonSession commonSes = HttpContext.Current.Session["CommonSession"] as CommonSession;
        if (commonSes != null)
            commonSes.bForce = false;

        iCustomDAO.Customer oldValues = this.Customers.Find(x => x.intCodeID == newValues.intCodeID);
        this.Customers.Remove(oldValues);

        if (commonSes.arrDeleted == null)
            commonSes.arrDeleted = new List<int>();
        commonSes.arrDeleted.Add(newValues.intCodeID);
    }

    /// <summary>
    /// Insert Items
    /// </summary>
    /// <param name="customer"></param>
    public void Insert(iCustomDAO.Customer newValues)
    {
        CommonSession commonSes = HttpContext.Current.Session["CommonSession"] as CommonSession;
        if (commonSes != null)
            commonSes.bForce = false;

        int tempID = Convert.ToInt32(this.Customers[this.Customers.Count - 1].intCodeID) + 1;
        newValues.intCodeID = tempID;
        newValues.strStatus = "Inserted";
        this.Customers.Add(newValues);
    }

    /// <summary>
    /// Return number of Items
    /// </summary>
    /// <param name="customer"></param>
    public int getCountItems()
    {
        return this.Customers.Count;
    }
}